package cn.wit.summit.chwan.widgets;

import android.app.Activity;
import android.content.Context;
import android.net.Uri;
import android.support.annotation.Nullable;
import android.util.AttributeSet;
import android.view.Gravity;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.facebook.drawee.view.SimpleDraweeView;
import com.google.gson.Gson;

import java.util.List;

import cn.wit.summit.chwan.R;
import cn.wit.summit.chwan.models.db.DownLoadTaskTable;
import cn.wit.summit.chwan.models.dbmanager.DownloadTaskManager;
import cn.wit.summit.chwan.models.dto.GameListBaseBean;
import cn.wit.summit.chwan.models.dto.GamePrizesBean;
import cn.wit.summit.chwan.tools.downloadtool.DownloadFactory;
import cn.wit.summit.chwan.utils.IntentUtil;
import cn.wit.summit.chwan.utils.StringUtils;
import cn.wit.summit.chwan.utils.UtilsMy;

/**
 * Created by Alan on 2018/3/13.
 */

public class ItemUpdateView extends LinearLayout implements View.OnClickListener {

    private Gson mGson = null;
    private Context mContext = null;
    private DownloadFactory factory;
    private LayoutParams params;
    private Uri uri;
    private DownLoadTaskTable mDownLoadTaskTable = null;
    private ItemDownloadInterface mItemDownloadInterface;

    private RelativeLayout itemDownloadRootRl;
    private SimpleDraweeView itemDownloadImg; //任务图片
    private TextView itemDownloadNameTx;      //任务名字
    private TextView itemDownloadSizeTx;      //热度和下载大小
    private LinearLayout itemDownloadDescLl;  //任务简介布局，非下载的时候显示
    private LinearLayout itemDownloadTagsLl;  //任务标签布局，会动态加入标签
    private TextView itemDownloadDescTx;      //任务简介
    private TextView cancleUpdate;      //任务简介
    private LinearLayout itemTagInfoLl;   //任务标签
    private DownloadViewStroke itemDownloadBtn;       //任务控制按钮
    private TextView win;      //任务简介

    public ItemUpdateView(Context context, @Nullable AttributeSet attrs) {
        super(context, attrs);
        initView(context, attrs);
    }

    public ItemUpdateView(Context context, @Nullable AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        initView(context, attrs);
    }

    public void setOnclickLinstener(ItemDownloadInterface itemDownloadInterface) {
        mItemDownloadInterface = itemDownloadInterface;
    }

    private void initView(Context context, AttributeSet attrs) {
        this.mContext = context;
        mGson = new Gson();
        uri = Uri.parse("res:///" + R.drawable.main_normal_icon);
        params = new LayoutParams(
                LayoutParams.WRAP_CONTENT, LayoutParams.WRAP_CONTENT);
        params.setMargins(5, 0, 5, 0);
        params.gravity = Gravity.CENTER;
        factory = DownloadFactory.get();

        View rootView = LayoutInflater.from(context).inflate(R.layout.item_update_view, null);
        itemDownloadRootRl = rootView.findViewById(R.id.itemDownloadRootRl);
        itemDownloadImg = rootView.findViewById(R.id.itemDownloadImg);
        itemDownloadNameTx = rootView.findViewById(R.id.itemDownloadNameTx);
        itemDownloadSizeTx = rootView.findViewById(R.id.itemDownloadSizeTx);
        itemDownloadDescLl = rootView.findViewById(R.id.itemDownloadDescLl);
        itemDownloadTagsLl = rootView.findViewById(R.id.itemDownloadTagsLl);
        itemDownloadDescTx = rootView.findViewById(R.id.itemDownloadDescTx);
        win = rootView.findViewById(R.id.win);
        cancleUpdate = rootView.findViewById(R.id.cancleUpdate);
        itemTagInfoLl = rootView.findViewById(R.id.itemTagInfoLl);

        itemDownloadBtn = rootView.findViewById(R.id.itemDownloadBtn);

        itemDownloadBtn.setOnClickListener(this);
        itemDownloadRootRl.setOnClickListener(this);
        cancleUpdate.setOnClickListener(this);
        itemDownloadRootRl.setOnLongClickListener(new OnLongClickListener() {
            @Override
            public boolean onLongClick(View v) {
                if (mItemDownloadInterface != null) {
                    mItemDownloadInterface.onAdapterItemLongChildClick(v, mDownLoadTaskTable);
                    return true;
                } else {
                    return false;
                }
            }
        });

        this.removeAllViews();
        this.addView(rootView);
    }

    GameListBaseBean gameListBaseBean;
    Activity activity;

    public void setData(Activity activity, GameListBaseBean gameListBaseBean) {
        this.activity = activity;
        setData(gameListBaseBean, true);
    }

    public void setDataNotagInfo(GameListBaseBean gameListBaseBean) {
        setData(gameListBaseBean, false);

    }

    private void setData(GameListBaseBean gameListBaseBean, boolean showTagInfo) {
        this.gameListBaseBean = gameListBaseBean;
        itemDownloadImg.setImageURI(gameListBaseBean.getGame_ico());
        itemDownloadNameTx.setText(gameListBaseBean.getGame_name());
        itemDownloadDescTx.setText(gameListBaseBean.getGame_info());
        itemDownloadSizeTx.setText(gameListBaseBean.getGame_size() + "M");
        if (showTagInfo) {
            itemTagInfoLl.setVisibility(VISIBLE);
            itemDownloadDescTx.setVisibility(GONE);
            UtilsMy.addTipsInView(gameListBaseBean.getGame_tag_info_v2(), itemTagInfoLl, getContext());
        } else {
            itemTagInfoLl.setVisibility(GONE);
            itemDownloadDescTx.setVisibility(VISIBLE);
            itemDownloadDescTx.setText(gameListBaseBean.getGame_info());
        }
//        DownLoadTaskTable taskTable= DownloadTaskManager.getInstance().findByGameId(gameListBaseBean.getGame_id());
//        if (taskTable!=null){
//            gameListBaseBean.setTaskTable(taskTable);
//        }
        itemDownloadBtn.initUpdateData(gameListBaseBean);
        String acive = gameListBaseBean.getGame_achievement_title();
        List<GamePrizesBean> prizesBeans = gameListBaseBean.getGame_prizes();
        if (prizesBeans!=null && prizesBeans.size()>0) {
            acive = prizesBeans.get(0).getTitle();
        }
        if (StringUtils.isNotEmpty(acive)) {
            win.setText(acive);
            win.setVisibility(VISIBLE);
        } else {
            win.setVisibility(GONE);
        }
    }

    public DownloadViewStroke getDownView() {
        return itemDownloadBtn;
    }

    @Override
    public void onClick(View v) {
        switch (v.getId()) {

            case R.id.itemDownloadRootRl://item点击

                IntentUtil.getInstance().goGamedetial(activity, gameListBaseBean.getGame_id());
                break;
            case R.id.cancleUpdate:
                v.setVisibility(GONE);
                gameListBaseBean.getTaskTable().setNotUpdateVersion(gameListBaseBean.getGame_ver());
                DownloadTaskManager.getInstance().updateUPStatusBygameId(gameListBaseBean.getGame_id(), gameListBaseBean.getGame_ver());
                mItemDownloadInterface.dismissItem(this, gameListBaseBean.getTaskTable());
                break;
        }
    }

    public interface ItemDownloadInterface {
        void onAdapterItemLongChildClick(View view, DownLoadTaskTable taskTable);

        void dismissItem(View view, DownLoadTaskTable taskTable);
    }
}
